<?php
$page=isset($_GET['page']) ? $_GET['page'] : 1;

$type = isset($_GET['no_type'])?$_GET['no_type']:"";
$where="no_type like '%$type%'";

$redis=new Redis();
$redis->connect('127.0.0.1',6379);
if($redis->hExists('week3',"page$page"))
{
    echo "缓存";
    $result = json_decode($redis->hGet('week3',"page$page"),true);
}
else
{
    echo "数据库";
    $dbh = new PDO('mysql:host=127.0.0.1;dbname=1705phpa', 'root', 'root');
    $sql="select * from week3 where $where";
    $res=$dbh->query($sql);

    $count_num=$res->rowCount();//总条数
    $size=2;//每页显示条数
    $page_count=ceil($count_num/$size);//接收页码
    $limit=($page-1)*$size;//计算偏移量

    $prev=$page-1>1 ? $page-1 : 1;
    $next = $page+1 < $page_count ? $page+1 : $page_count;
    $sql="select * from week3  where $where limit $limit,$size";
    $arr=$dbh->query($sql);
    $data=$arr->fetchAll(2);

    $result['data']=$data;
    $result['prev']=$prev;
    $result['next']=$next;
    $result['count_num']=$count_num;

    $redis->hSet("week3","page$page",json_encode($result));
}

?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <link rel="stylesheet" href="layui/css/layui.css">
    <script src="layui/layui.js"></script>
</head>
<body>

<form>
    类型：<input type="text" name="no_type">
    <input type="submit" value="搜索">
</form>

<center>

    <table class="layui-table" lay-skin="line">
        <colgroup>
            <col width="150">
            <col width="150">
            <col width="150">
            <col>
        </colgroup>
        <thead>
        <tr>
            <th>地址</th>
            <th>书本类型</th>
            <th>书本名字</th>
            <th>书本数量</th>
            <th>作者</th>
        </tr>
        <?php
        foreach($result['data'] as $k=>$v)
        {
        ?>
            <tr>
                <td><?php echo $v['no_id']?></td>
                <td><?php echo $v['no_type']?></td>
                <td><?php echo $v['no_name']?></td>
                <td><?php echo $v['no_num']?></td>
                <td><?php echo $v['no_author']?></td>
            </tr>

            <?php
        }
        ?>
    </table>

    <a href="show2.php? page=1">首页</a>
    <a href="show2.php? page=<?php echo $result['prev']?>">上一页</a>
    <a href="show2.php? page=<?php echo $result['next']?>">下一页</a>
    <a href="show2.php? page=<?php echo $result['count_num']?>">尾页</a>
</center>
</body>
</html>
